package com.silverhaze.diamond.client.ui.dialogs;

import com.silverhaze.commons.helpers.CommonUIHelper;
import com.silverhaze.commons.ui.simpledatepicker.SimpleDatePicker;
import com.silverhaze.commons.ui.simpledatepicker.SimpleDatePickerListener;
import com.silverhaze.commons.ui.simpledatepicker.lang.RussianSimpleDatePicker;
import com.silverhaze.commons.utils.DateUtils;
import com.silverhaze.commons.utils.collections.CollectionsUtils;
import com.silverhaze.diamond.client.data.SpecificData;
import com.silverhaze.diamond.client.data.dao.DAOLocator;
import com.silverhaze.diamond.client.resources.ServiceResourceContainer;
import com.silverhaze.diamond.client.ui.UIHelper;
import com.silverhaze.diamond.client.ui.table.ServiceTable;
import com.silverhaze.diamond.client.ui.table.model.ServiceTableModel;
import com.silverhaze.diamond.common.entity.Service;

import javax.swing.*;
import java.awt.*;
import java.util.Date;
import java.util.List;

/**
 * SystemUser: Babanin
 * Date: 05.02.2009
 * Time: 12:45:47
 */
public class StatisticsDialog extends AbstractDialog {

    private ServiceTable serviceTable;
    private SimpleDatePicker beginSimpleDatePicker;
    private SimpleDatePicker endSimpleDatePicker;
    private JLabel serviceCostLabel;
    private JLabel serviceCountLabel;

    private Date startDate;
    private Date endDate;

    private JLabel errorLabel;
    private JPanel errorPanel;

    public StatisticsDialog() {
        super(null, "Статистика по сервисам", true);
        initializeOkCancelPanel(DialogType.VIEW);
        setIconImage(ServiceResourceContainer.SERVICE.getImage());

        initView();
    }

    private double countCost(List<Service> data) {
        double result = 0;

        for (Service service : data) {
            result += service.getCost();
        }

        return result;
    }

    private void initView() {
        startDate = DateUtils.midnight(DateUtils.dateMinusDays(new Date(), 1));
        endDate = DateUtils.midnight(DateUtils.datePlusDays(new Date(), 1));

        java.util.List filteredServices = SpecificData.filterServices("*", "*", "*", null, startDate, endDate, true, false, DAOLocator.getInstance().getServices());
        serviceTable = new ServiceTable(new ServiceTableModel(filteredServices));
        beginSimpleDatePicker = new SimpleDatePicker(startDate, new RussianSimpleDatePicker());
        endSimpleDatePicker = new SimpleDatePicker(endDate, new RussianSimpleDatePicker());

        serviceCountLabel = new JLabel("<html><b>Сервисов за период: " + filteredServices.size() + "</b></html>");
        serviceCostLabel = new JLabel("<html><b>Общая стоимость: " + countCost(filteredServices) + " р. </b></html>");

        beginSimpleDatePicker.addListener(new SimpleDatePickerListener() {
            public void dateChanged(Date newDate) {
                if (DateUtils.compareDates(newDate, endDate) > 0) {
                    errorLabel.setText("<html><font color=red><b>Ошибка: </b> Начало периода не может быть больше конца периода</font></html>");
                    errorPanel.setVisible(true);
                    beginSimpleDatePicker.setDate(startDate);
                } else {
                    startDate = newDate;
                    updateData();
                    errorPanel.setVisible(false);
                }
            }
        });

        endSimpleDatePicker.addListener(new SimpleDatePickerListener() {
            public void dateChanged(Date newDate) {
                if (DateUtils.compareDates(newDate, startDate) < 0) {
                    errorLabel.setText("<html><font color=red><b>Ошибка: </b> Конец периода не может быть раньше начала периода</font></html>");
                    errorPanel.setVisible(true);
                    endSimpleDatePicker.setDate(endDate);
                } else {
                    endDate = newDate;
                    updateData();
                    errorPanel.setVisible(false);
                }
            }
        });

        errorLabel = new JLabel();
        errorPanel = CommonUIHelper.packInPanel(errorLabel, BorderLayout.LINE_START);
        errorPanel.setBorder(BorderFactory.createEmptyBorder(7, 5, 7, 0));
        errorPanel.setVisible(false);

        add(UIHelper.packInShiftedPanel(UIHelper.vBox(
                UIHelper.hBox(3, "Период:", 4, beginSimpleDatePicker, " - ", endSimpleDatePicker),
                errorPanel
        ), 0, 5, 0, 0), BorderLayout.NORTH);

        add(CommonUIHelper.createVerticalBox(
                new JScrollPane(serviceTable),
                5,
                CommonUIHelper.createBorderLayoutPanel(CollectionsUtils.array(serviceCountLabel, serviceCostLabel),
                        CollectionsUtils.array(BorderLayout.LINE_START, BorderLayout.LINE_END))
        ), BorderLayout.CENTER);

        getRootPane().setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        setPreferredSize(new Dimension(800, 400));

        pack();

        setLocationRelativeTo(UIHelper.getMainFrame());
    }

    private void updateData() {
        java.util.List filteredServices = SpecificData.filterServices("*", "*", "*", null, startDate, endDate,
                true, false, DAOLocator.getInstance().getServices());

        serviceTable.setModel(new ServiceTableModel(filteredServices));

        serviceCountLabel.setText("<html><b>Сервисов за период: " + filteredServices.size() + "</b></html>");
        serviceCostLabel.setText("<html><b>Общая стоимость: " + countCost(filteredServices) + " р. </b></html>");
    }

    protected void okPressed() {
        dispose();
    }
}

